<?php 
	include_once('../config.php'); 
	session_start();
?>
<script type="text/javascript">
	
	function PageChanged(PageIndex) {
		var year = $("#year-droplist").text();
		var season = $("#season-droplist").text();
		
		//Call self.
		$("#content-detail").fadeOut('slow').load('../sheetlist/index.php?year='+year+'&season='+season+'&Page='+PageIndex, null,
			function (responseText, textStatus, XMLHttpRequest) {
   				if (textStatus == "success")
					$("#content-detail").fadeIn('slow');
    			else if (textStatus == "error")
         			console.log("error");
			}
		);
	}
	
	function selAll(){
		var checkItem = document.getElementsByName("sheet[]");
		for(var i=0;i<checkItem.length;i++){
			checkItem[i].checked=true;   
		}
	}
	
	function DataDeleted(TableName, Page) {
		
		$("#dialog-confirm").dialog({
      		resizable: false,
      		height:160,
      		modal: true,
      		buttons: {
        		"Delete": function() {
					
					var year = $("#year-droplist").text();
					var season = $("#season-droplist").text();
					
					//Call self.
					$("#content-detail").fadeOut('slow').load('../sheetlist/index.php?year='+year+'&season='+season+'&Page='+Page, 
							{'TableName': TableName}, 
							function (responseText, textStatus, XMLHttpRequest) {
   								if (textStatus == "success")
									$("#content-detail").fadeIn('slow');
    							else if (textStatus == "error")
         							console.log("error");
							}
						);
					$( this ).dialog( "close" );
       			},
        		Cancel: function() {
          			$( this ).dialog( "close" );
       	 		}
			},
			open: function (event, ui) {
    			$('#dialog-confirm').css('overflow', 'hidden'); //this line does the actual hiding
  			}
		});
	}
	
	function DeselAll(){
		$( "#dialog-confirm" ).dialog({
      		resizable: false,
      		height:160,
      		modal: true,
      		buttons: {
        		"Delete": function() {
					//Get CheckBox values.
					var selectedValues = $("input:checkbox:checked").map(function(){
      					return $(this).val();
    				}).get();
					
					if (selectedValues.length!=0) {
						
						var year = $("#year-droplist").text();
						var season = $("#season-droplist").text();
					
						//Call self.
						$("#content-detail").fadeOut('slow').load('../sheetlist/index.php?year='+year+'&season='+season+'&Page=<?php echo $_GET['Page']; ?>', 
							{'sheet': selectedValues}, 
							function (responseText, textStatus, XMLHttpRequest) {
   								if (textStatus == "success")
									$("#content-detail").fadeIn('slow');
    							else if (textStatus == "error")
         							console.log("error");
							}
						);
					}
					
					$( this ).dialog( "close" );
       			},
        		Cancel: function() {
          			$( this ).dialog( "close" );
       	 		}
      		},
			open: function (event, ui) {
    			$('#dialog-confirm').css('overflow', 'hidden'); //this line does the actual hiding
  			}
    	});
	}
	
</script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />

<!----- Dialog Confirm ------>
<div id="dialog-confirm" title="Alert" hidden="YES">
  <p><span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>Delete data. Are you sure?</p>
</div>

	<div id="sheet-list">
<div class="container2">
  <div class="content">
  <a href="#"><img src="../images/menu.png" width="18" height="18" border="0" class="menu"/></a><span class="table-of-contents"><a href="#">Table of Contents</a></span>
  <button class="button selectall white" onclick="selAll();">Select All</button>
  <button class="button deselect white" onclick="DeselAll();">Deselect All</button>
  <?php
	   if($_GET['year'] !="")
	   {
	     PageChange();
	   }
	   else
	   {
	     //null
	   }
  ?>
  </div>
  <!-- end .container --></div>

  <?php
  	
	LinkDataBase();
	//Delete data.
	if (isset($_POST['sheet'])) {
		
		$sheet = $_POST ["sheet"];
		$SheetCounter = 0;

		while($sheet[$SheetCounter] != NULL)
		{
			$sql = "select filename from sheet where filename LIKE '%".$_GET['year']."%' AND filename LIKE '%".$_GET['season']."%'";
			$result= mysql_query($sql);
			while($row = mysql_fetch_assoc($result)) {
				$sql = "delete from sheet where table_name ='".$sheet[$SheetCounter]."' AND filename='".$row['filename']."'";
				$result= mysql_query($sql);
				$sql = "delete from ".$row['filename']." where table_name ='".$sheet[$SheetCounter]."'";
				$result= mysql_query($sql);
			}
			
			$SheetCounter++;
		}
	}
	else if (isset($_POST['TableName'])) {
	
		$sql = "select filename from sheet where filename LIKE '%".$_GET['year']."%' AND filename LIKE '%".$_GET['season']."%'";
		$result= mysql_query($sql);
		while($row = mysql_fetch_assoc($result)) {
			$sql = "delete from sheet where table_name ='".$_POST["TableName"]."' AND filename='".$row['filename']."'";
			$result= mysql_query($sql);
			$sql = "delete from ".$row['filename']." where table_name ='".$_POST["TableName"]."'";
			$result= mysql_query($sql);
		}
	}
  
	if(isset($_GET['year']))
	{
		$OutputRange = (($_GET['Page']-1)*10);
		$sql = "select table_number,table_name,filename from sheet where filename LIKE '%".$_GET['year']."%' AND filename LIKE '%".$_GET['season']."%' limit ".$OutputRange.",10";
		 
		$result= mysql_query($sql);
		$TCount = 1;
		while($row = mysql_fetch_assoc($result))
		{
			echo '<div class="container3">';
			echo '<div class="qaimage">';
			echo '<div class="checkbox'.$TCount.'" style="float:left;">';
			echo '<input name="sheet[]" type="checkbox" value="'.$row['table_name'].'" id="checkbox'.$TCount.'" />';			
			echo '<label for="checkbox'.$TCount.'"></label>';
			echo '</div>';
			echo '<div style="width:750px;margin-left:5px;float:left;"><span class="qaquestion">'.$row['table_name'].'</span></div>';
			echo '<a href="../table/index.php?sheet='.$row['table_name']."&filename=".$row['filename']."&Page=".$_GET['Page'].
			'" class="view">View</a><a href="#" onclick="DataDeleted(\''.$row['table_name'].'\', '.$_GET['Page'].');" class="delete">Delete</a>';
			echo '</div>';
			echo '</div>';
			$TCount++;
		}
	}
  ?>
  </div>
  
  
<?php

function PageChange()
{
  if(isset($_GET['Page']))
  {
	LinkDataBase();
	$sql = "select table_number from sheet where filename LIKE '%".$_GET['year']."%' AND filename LIKE '%".$_GET['season']."%'";
	$result= mysql_query($sql);
	$PageCounter = 0;
	while($row = mysql_fetch_assoc($result))
	{
		$PageCounter++;
	}
	$PageCounter = floor($PageCounter / 10) +1;
	if($_GET['Page'] != 1)
	{
		echo '<a href="#" onclick="PageChanged('.($_GET['Page']-1).');" ><img src="../images/prev-button.png" width="23" height="23" class="leftarrow" /></a>';
	}
	else
	{
		echo '<a href="#" onclick="PageChanged('.($_GET['Page']).');" ><img src="../images/prev-button.png" width="23" height="23" class="leftarrow" /></a>';
	}
	
	echo '<span class="link">';
	if($_GET['Page'] != 1)
	{
		echo '<a href="#" onclick="PageChanged('.($_GET['Page']-1).');" class="link">Prev</a>';
	}
	else
	{
		echo '<a href="#" onclick="PageChanged('.($_GET['Page']).');" class="link">Prev</a>';
	}
		
	if($_GET['Page'] < 3)
	{
		$Low_Page = 1;
		if(($_GET['Page']+4) >= $PageCounter)
		{
			$Up_Page = $PageCounter;
		}
		else
		{
			$Up_Page = 5;
		}
	}
	else if($PageCounter - $_GET['Page'] < 3)
	{
		$Up_Page = $PageCounter;
		if(($PageCounter-5) <= 0)
		{
			$Low_Page = 1;
		}
		else
		{
			$Low_Page = floor($PageCounter - 4);
		}
	}
	else
	{
		$Low_Page = $_GET['Page'] - 2;
		if(($_GET['Page']+2) >= $PageCounter)
		{
			$Up_Page = $PageCounter;
		}
		else
		{
			$Up_Page = $_GET['Page']+2;
		}				
	}
	for($i=$Low_Page;$i<=$Up_Page;$i++)
	{
		if($i == $_GET['Page'])
		{
			echo '<a href="#" onclick="PageChanged('.$i.');" class="link">'.$i.'</a>';
			echo "   ";
		}
		else
		{
			echo '<a href="#" onclick="PageChanged('.$i.');" class="link"><u>'.$i.'</u></a>';
			echo "   ";
		}
	}
	
	if($_GET['Page'] != $PageCounter)
	{
		echo '<a href="#" onclick="PageChanged('.($_GET['Page']+1).');" class="link">Next</a>';
	}
	else
	{
		echo '<a href="#" onclick="PageChanged('.($_GET['Page']).');" class="link">Next</a>';
	}
		
	echo '</span> ';
	
	if($_GET['Page'] != $PageCounter)
	{
  	    echo '<a href="#" onclick="PageChanged('.($_GET['Page']+1).');"><img src="../images/next-button.png" width="23" height="23" class="rightarrow" /></a>';
	}
	else
	{
		echo '<a href="#" onclick="PageChanged('.($_GET['Page']).');"><img src="../images/next-button.png" width="23" height="23" class="rightarrow" /></a>';
	}
  }
}
?>
